﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using DAO;
using System.Collections;
using System.IO;
using System.Diagnostics;

namespace QuanLyNhaSach_1173005
{
    public partial class frmDanhSachSach : Form
    {
        public frmDanhSachSach()
        {
            InitializeComponent();
        }
        private void LoadSach()
        {
            dtgv_HienThiSach.Rows.Clear();
            ArrayList arr = SachBUS.LayDanhSach();
            for (int i = 0; i < arr.Count; i++)
            {
                SachDTO dv = (SachDTO)arr[i];
                DataGridViewRow row = new DataGridViewRow();
                DataGridViewTextBoxCell cellSTT = new DataGridViewTextBoxCell();
                cellSTT.Value = i + 1;
                DataGridViewTextBoxCell cellMaSach = new DataGridViewTextBoxCell();
                cellMaSach.Value = dv.MaSach;
                DataGridViewTextBoxCell cellTenSach = new DataGridViewTextBoxCell();
                cellTenSach.Value = dv.TenSach;
                DataGridViewTextBoxCell cellTheLoai = new DataGridViewTextBoxCell();
                cellTheLoai.Value = dv.TheLoai;
                DataGridViewTextBoxCell celltacGia = new DataGridViewTextBoxCell();
                celltacGia.Value = dv.TacGia;
                DataGridViewTextBoxCell cellSLTon = new DataGridViewTextBoxCell();
                cellSLTon.Value = dv.SoLuongTon;
                DataGridViewTextBoxCell cellDonGia = new DataGridViewTextBoxCell();
                cellDonGia.Value = dv.DonGiaBan;
                row.Cells.Add(cellSTT);
                row.Cells.Add(cellMaSach);
                row.Cells.Add(cellTenSach);
                row.Cells.Add(cellTheLoai);
                row.Cells.Add(celltacGia);
                row.Cells.Add(cellSLTon);
                row.Cells.Add(cellDonGia);
                dtgv_HienThiSach.Rows.Add(row);
            }
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            SachDTO sach = new SachDTO();
            sach.MaSach = "S" + DateTime.Now.Ticks;
            sach.TenSach = txt_TenSach.Text;
            sach.TheLoai = txt_TheLoai.Text;
            sach.TacGia = txt_TacGia.Text;
            sach.DonGiaBan = float.Parse(txt_Gia.Text);
            sach.SoLuongTon = 0;
            SachBUS.ThemSach(sach);
            MessageBox.Show("Thêm sách thành công", "Quản lý nhà sách");
            LoadSach();
        }

        private void frmSach_Load(object sender, EventArgs e)
        {
            LoadSach();
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            if (dtgv_HienThiSach.SelectedRows.Count > 0)
            {
                String masach = dtgv_HienThiSach.SelectedRows[0].Cells["MaSach"].Value.ToString();
                SachDTO sach = new SachDTO();
                sach.MaSach = txt_MaSach.Text;
                sach.TenSach = txt_TenSach.Text;
                sach.TheLoai = txt_TheLoai.Text;
                sach.TacGia = txt_TacGia.Text;
                sach.DonGiaBan = float.Parse(txt_Gia.Text);

                SachBUS.CapNhatSach(sach);
                MessageBox.Show("Cập nhật thông tin sách thành công", "Quản lý nhà sách");
                LoadSach();
            }
            else
            {
                MessageBox.Show("Chưa chọn sách để cập nhật", "Quản lý nhà sách");
            }
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            if (dtgv_HienThiSach.SelectedRows.Count > 0)
            {
                String masach = dtgv_HienThiSach.SelectedRows[0].Cells["MaSach"].Value.ToString();
                SachDTO sach = new SachDTO();
                sach.MaSach = masach;
                SachBUS.XoaSach(sach);
                MessageBox.Show("Xóa sách thành công", "Quản lý nhà sách");
                LoadSach();
            }
            else
            {
                MessageBox.Show("Chưa chọn sách để xóa", "Quản lý nhà sách");
            }
        }

        private void btn_PhieuMoi_Click(object sender, EventArgs e)
        {
            txt_Gia.Text = "";
            txt_MaSach.Text = "";
            txt_TacGia.Text = "";
            txt_TenSach.Text = "";
            txt_TheLoai.Text = "";

        }

        private void dtgv_HienThiSach_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgv_HienThiSach.SelectedRows.Count > 0)
            {
                String masach = dtgv_HienThiSach.SelectedRows[0].Cells["MaSach"].Value.ToString();
                SachDTO sach = SachBUS.LayThongTinSach(masach);
                txt_MaSach.Text = sach.MaSach;
                txt_TenSach.Text = sach.TenSach;
                txt_TheLoai.Text = sach.TheLoai.ToString();
                txt_TacGia.Text = sach.TacGia;
                txt_Gia.Text = sach.DonGiaBan.ToString();

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string oldPath = Directory.GetCurrentDirectory();
            SaveFileDialog sf = new SaveFileDialog();
            sf.ShowDialog();
            string strFileName = sf.FileName;
            Directory.SetCurrentDirectory(oldPath);
            ExcelWrite myExcel = new ExcelWrite();
            List<String> list = myExcel.DGVToExcel(dtgv_HienThiSach);
            String filePath = Directory.GetCurrentDirectory() + strFileName;
            myExcel.WriteDateToExcel(strFileName, list, "A1", "G1", "A2");
            MessageBox.Show("Export dữ liệu thành công...", "Thông báo");
        }

      /*  private void btn_import_Click(object sender, EventArgs e)
        {
            string oldPath = Directory.GetCurrentDirectory();
            OpenFileDialog of = new OpenFileDialog();
            of.ShowDialog();
            string strPathName = of.FileName;
            Directory.SetCurrentDirectory(oldPath);
            DataTable dt = ExcelRead.getSheet(strPathName, "sheet1");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                SachDTO s = new SachDTO();
                s.STT = i + 1;
                s.MaSach = Convert.ToString(dt.Rows[i]["MaSach"]);
                s.TenSach = Convert.ToString(dt.Rows[i]["TenSach"]);
                s.TheLoai = Convert.ToString(dt.Rows[i]["TheLoai"]);
                s.TacGia = Convert.ToString(dt.Rows[i]["TacGia"]);
                s.SoLuongTon = Convert.ToInt32(dt.Rows[i]["SoLuongTon"]);
                s.DonGiaBan = float.Parse(dt.Rows[i]["GiaBan"].ToString());             
                //s.TrangThai = Convert.ToInt32(dt.Rows[i]["TrangThai"]);
                
                SachBUS.ThemSach(s);
            }
            MessageBox.Show("Import dữ liệu thành công ...", "Thông báo");
            LoadSach();
        }*/
    }
}
